package com.csust.automaticteach.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csust.automaticteach.common.BaseResponse;
import com.csust.automaticteach.common.ResultUtils;
import com.csust.automaticteach.model.entity.ChatHistory;
import com.csust.automaticteach.model.entity.User;
import com.csust.automaticteach.service.ChatHistoryService;
import com.csust.automaticteach.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @ ClassName ChatHistoryController
 * @ Description 对话历史控制器
 * @ Author 钰玟
 * @ Date 2025/9/27 下午2:07
 * Version 1.0
 **/
@RestController
@RequestMapping("/chatHistory")
public class ChatHistoryController {

    private final ChatHistoryService chatHistoryService;
    private final UserService userService;

    public ChatHistoryController(ChatHistoryService chatHistoryService, UserService userService) {
        this.chatHistoryService = chatHistoryService;
        this.userService = userService;
    }

    /**
     * 分页查询某个应用的对话历史（游标查询）
     *
     * @param bizId          应用ID
     * @param pageSize       页面大小
     * @param lastCreateTime 最后一条记录的创建时间
     * @param request        请求
     * @return 对话历史分页
     */
    @GetMapping("/biz/{bizId}")
    public BaseResponse<Page<ChatHistory>> listAppChatHistory(@PathVariable Long bizId,
                                                              @RequestParam Integer bizType,
                                                              @RequestParam(defaultValue = "10") int pageSize,
                                                              @RequestParam(required = false)
                                                              @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
                                                              LocalDateTime lastCreateTime,
                                                              HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if(lastCreateTime != null) {
            lastCreateTime = lastCreateTime.plusHours(8);
        }
        Page<ChatHistory> result = chatHistoryService.listAppChatHistoryByPage(bizId,bizType, pageSize, lastCreateTime, loginUser);
        return ResultUtils.success(result);
    }

}
